import os
from ctpbee import CtpBee, CtpbeeApi
from ctpbee.constant import TradeData, OrderData, ContractData, Exchange
import pandas as pd


class DemoStrategy(CtpbeeApi):
    def on_bar(self, bar):
        # 策略逻辑
        pass


folder_path = "futures_data/SHFE1117"
csv_files = [f for f in os.listdir(folder_path) if f.endswith('.csv')]

for csv_file in csv_files:
    data_path = os.path.join(folder_path, csv_file)

    data = pd.read_csv(data_path)
    # data = data.drop("Unnamed: 0", axis=1)
    data = [list(reversed(data.to_dict("index").values()))]

    # Create an instance of CtpBee and backtest
    app = CtpBee("demo", __name__)
    strategy = DemoStrategy("strategy", app)  # Replace YourStrategy with your actual strategy class
    code = csv_file.split('.')[0].join(['', '.SHFE'])  # e.g. 'rb2010.SHFE'
    info = {
        "PATTERN": "looper",
        "LOOPER": {
            "initial_capital": 100000,
            "margin_ratio": {
                code: 0.00003,
            },
            "commission_ratio": {
                code: {
                    "close": 0.00001
                },
            },
            "size_map": {
                code: 10
            }
        }
    }
    app.config.from_mapping(info)
    app.add_local_contract(ContractData(local_symbol=code, exchange=Exchange.SHFE, symbol=code.split(".")[0],
                                        size=10, pricetick=1))
    app.add_extension(strategy)
    app.add_data(*data)
    app.start()

    result = app.get_result(report=False, auto_open=True)
